package com.sservice.web.controller.pc;

import com.sservice.common.Constants;
import com.sservice.model.vo.OfferVo;
import com.sservice.service.service.IGoodsService;
import com.sservice.service.service.IOfferService;
import com.sservice.service.service.IUserService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.Goods;
import com.sservice.model.entity.Offer;
import com.sservice.banfu.e.SalePerson;
import com.sservice.urp.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/offer")
public class OfferPcController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IOfferService depService;
    @Autowired
    private IGoodsService goodService;
    @Autowired
    private IUserService userService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Map> get(Offer arg) {
        OfferVo service_res = depService.selectByPrimaryKey(arg.getId());
        Goods gds = goodService.selectByPrimaryKey(Integer.valueOf(service_res.getGoodsId()));
        User user = userService.selectByPrimaryKey(Integer.valueOf(service_res.getCreatorId()));
        Map map = new HashMap();
        map.put("goods", gds);
        map.put("offer", service_res);
        map.put("user", user);
        BaseResult<Map> res = new BaseResult<Map>(true, map);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/getOperators", method = RequestMethod.GET)
    public ListResult<Object> get() {
        List<User> gds = goodService.queryGoodsQQ();
        return new ListResult(true, gds);
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<OfferVo> query(OfferVo arg) {
        List<OfferVo> list = depService.queryByPagePC(arg);
        arg.setStatus("1");
        int total = depService.queryCountPC(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<OfferVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /**
     * 查找最新3个报价
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryLatest", method = RequestMethod.GET)
    public ListResult<OfferVo> queryLatest(OfferVo arg) {
        List<OfferVo> list = depService.queryByPagePC(arg);
        arg.setStatus("1");
        int total = depService.queryCountPC(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<OfferVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/focus", method = RequestMethod.GET)
    public ListResult focus(OfferVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);

        if (sp == null) {
            return new ListResult(false, "请登录!", 401);
        }
        arg.setUserId(String.valueOf(sp.getId()));
        List<OfferVo> list = depService.queryByPagePCFocus(arg);
        arg.setStatus("1");
        int total = depService.queryCountPCFocus(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<OfferVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
